import ajax from 'axios';
import qs from 'qs';
import global from "../../plugins/globalfunc";
const modules = {
  namespaced: true,
  state: {
    nowplaying: [],
    coming: [],
    detail: {},
    photoList: [],
    Wchoose: true,
    Fchoose: true,
    MymovDetail: [],
    movComment: [],
    upComment:[
      {avatarUrl:window.localStorage.getItem('head'),
      uid:window.localStorage.getItem('id'),
      nickName:window.localStorage.getItem('username'),
      reply:0,approve:0}],
    movFindByName:[],
    OldCom:[],
    MovCommentByUser:[],
    FavList:[],
    MovComList:[]
  },
  mutations: {
    MovNow(state, info) {
      state.nowplaying = info || [];
    },
    MovComing(state, info) {
      state.coming = info || [];
    },
    MovDetail(state, info) {
      state.detail = info || {};
      state.photoList=global.picFix(info.photos)
    },
    MovWatch(state, info) {
      state.Wchoose = info;
    },
    MovFav(state,info){
      state.Fchoose=info;
    },
    MovMyDetail(state,info){
      state.MymovDetail=info
    },
    MovComment(state,info){
      state.movComment=info;
    },
    MovlocalComment(state,info){
     info.map((item)=>{
       let con={...Object.assign(state.upComment[0],item)}
       state.movComment.cmts.push(con)
      })
      state.movComment.cmts=state.movComment.cmts.reverse()
    },
    MovCommentByUser(state,info){
      state.MovCommentByUser=info
    },
    MovaddComment(state,info){
      console.log(info)
      Object.assign(state.upComment[0],info)
    },
    movFindByName(state,info){
      state.movFindByName=info
    },
    OldCom(state,info){
      console.log(info)
      state.OldCom.push(info)
      sessionStorage["OldCom"] = JSON.stringify(state.OldCom);
    },
    getFavList(state,info){
      state.FavList=info
    },
    getMovComList(state,info){
      state.MovComList=info
    }

  },
  actions: {
    movNow({commit}) {  // 正在热映
      ajax.get('/api2/ajax/movieOnInfoList').then((res) => {
        commit('MovNow', res.data.movieList);
      }).catch((error) => {
        console.log(error);
      });
    },
    movComing({commit}) {  // 即将上映
      ajax.get('/api2/ajax/comingList?ci=10&token=&limit=100').then((res) => {
        commit('MovComing', res.data.coming);
      }).catch((error) => {
        console.log(error);
      });
    },
    movDetail({commit}, id) {  // 电影详情
      ajax.get('/api2/ajax/detailmovie?movieId=' + id).then((res) => {
        commit('MovDetail', res.data.detailMovie);
      }).catch((error) => {
        console.log(error);
      });
    },
    movWatch(context, param) {  // 电影想看
      ajax.post('/api/movie/index',qs.stringify(param)).then((res) => {
        context.commit('MovWatch', res.data.data.choose);
      }).catch((error) => {
        console.log(error);
      });
    },
    movFav(context, param) {  // 电影收藏
      ajax.post('/api/movie/index',qs.stringify(param)).then((res) => {
        context.commit('MovFav', res.data.data.choose);
      }).catch((error) => {
        console.log(error);
      });
    },
    movAddWatch(context,playload){//添加想看
    ajax.post('api/movie/addWatch',qs.stringify(playload)).then((res)=>{
      context.commit('MovWatch', res.data.data.choose);
    }).catch((error) => {
      console.log(error);
    });
    },
    movAddFav(context,playload){//添加收藏
      ajax.post('api/movie/addWatch',qs.stringify(playload)).then((res)=>{
        context.commit('MovFav', res.data.data.choose);
      }).catch((error) => {
        console.log(error);
      });
    },
    movSearchW({commit},playload){//查找我的想看
      ajax.post('api/movie/Watch',qs.stringify(playload)).then((res)=>{
        // commit('MovSearchW', res.data.data);
        var arr=[]
        res.data.data.map(item=>{
          ajax.get('/api2/ajax/detailmovie?movieId=' + item.movieid).then((res) => {
            arr.push(res.data.detailMovie)
            commit('MovMyDetail',arr);
            //console.log()
          }).catch((error) => {
            console.log(error);
          });
        })
      }).catch((error) => {
        console.log(error);
      });
    },
    movSearchF(context,playload){//查找我的收藏
      ajax.post('api/movie/Watch',qs.stringify(playload)).then((res)=>{
        context.commit('MovSearchF', res.data.data.choose);
      }).catch((error) => {
        console.log(error);
      });
    },

    movComment({commit}, id) {  // 电影详情评论
      ajax.get('/api2/mmdb/comments/movie/' + id+'.json').then((res) => {
        commit('MovComment', res.data);
      }).catch((error) => {
        console.log(error);
      });
      ajax.get('/api/Comment/getCommentbyMovie?Id='+ id).then((res) => {
        commit('MovlocalComment', res.data.data);
      }).catch((error) => {
        console.log(error);
      });
    },
    movLocalComByUser({commit},id) {
      ajax.get('/api/Comment/getMovCommentbyUser?uid='+id).then((res) => {
        commit('MovCommentByUser', res.data.data);
      }).catch((error) => {
        console.log(error);
      });
    },
    movAddComment({commit},addComment){//添加评论
      let allAdd=Object.assign(this.state.movie.upComment[0],addComment)
console.log(allAdd,addComment,this.state.movie.upComment[0])
      if(!addComment.userId){
        ajax.post('api/Comment/MovieComment',allAdd).then((res)=>{
          commit('MovaddComment',res.data.data);
        }).catch((error)=>{console.log(error)})
      }else {
        commit('OldCom',addComment)
      }

    },
    movFindByName({commit},searchName){
      ajax.get('/api2/ajax/movieOnInfoList').then((res)=>{
        var resList=[]
        res.data.movieList.map((item,i)=>{
          if(item.nm.search(searchName)===-1){
            return false}else {
            resList.push(res.data.movieList[i])}
        })
        commit('movFindByName',resList);
      }).catch((error)=>{console.log(error)})
    },
    getFavList({commit}) { //管理员 收藏
      ajax.get('/api/Movie/getList').then((res) => {
            commit('getFavList',res.data.data);
      }).catch((error) => {
        console.log(error);
      });
    },
    getMovComList({commit},playload) {//管理员 评论
      if(!playload){playload=''}
      ajax.get('/api/Comment/getMovCommentList?query='+playload).then((res) => {
        commit('getMovComList',res.data.data);
      }).catch((error) => {
        console.log(error);
      });
    },

  }
}
export default modules
